<?php if ( ! DEFINED('BASEPATH') ) die('No direct script access allowed.');

/**
 * @author 		Mark Skilbeck
 * @copyright 	Copyright (c) 2009, Mahcuz.com, Inc.
 */

class Release extends Controller {
	
	function Release ()
	{
		parent::Controller();
		// Load necessary libraries, models, etc.
			$this->load->model('project_model');
			// Call methods for theme statically!
			$this->load->library('theme');
			$this->load->library('form_validation');
		
		// Load profiler - debugging only.
			$this->output->enable_profiler(TRUE);
			
		// Initialise Theme and load theme.
			Theme::init();
			Theme::load_theme_type('admin');
	}
	
	function index ()
	{
		$vars['area'] = 'admin';
		$vars['module'] = 'release/main';
		$vars['theme_type'] = Theme::$theme_type;
		$vars['theme_alias'] = Theme::$theme_alias;
		$this->load->view('loader', $vars);
	}
	
	function add ()
	{
		$vars['area'] = 'admin';
		$vars['module'] = 'release/add';
		$vars['theme_type'] = Theme::$theme_type;
		$vars['theme_alias'] = Theme::$theme_alias;
		
		# We have a project alias.
		if ($this->uri->segment(4) !== FALSE) 
		{
			if ( $this->project_model->name === NULL )
			{
				show_error('That project does not exist.');
			}
			
			$vars['project'] = $this->project_model->get_project_by_alias($this->uri->segment(4));
			# Form has been submitted
			if ($this->input->post('add_release') !== FALSE) 
			{
				# Define some rules for validation
				$rules = array (
					array (
						'field' => 'project_name',
						'label' => 'Project Name',
						'rules' => 'required|trim|callback_project_exists'
					),
					array (
						'field' => 'release_codename',
						'label' => 'Release Codename',
						'rules' => 'trim|max_length[200]'
					),
					array (
						'field' => 'release_type',
						'label' => 'Release Type',
						'rules' => 'trim|callback_valid_release_type'
					),
					array (
						'field' => 'release_version',
						'label' => 'Release Version',
						'rules' => 'trim|required|max_length[20]'
					)
				);
				$this->form_validation->set_rules($rules);
				
				if ($this->form_validation->run() === FALSE)
				{
					$vars['release_count'] = $this->project_model->get_release_count_by_alias($this->uri->segment(4));
					$vars['page'] = 'add_form';
					$this->load->view('loader', $vars);
				}
				else
				{
					# Push uploaded file into /uploads/ folder, and add database information.
					try 
					{
						$data = array (
							'project_id' => $vars['project']->id,
							'release_version' => $this->input->post('release_version'),
							'release_codename' => $this->input->post('release_codename'),
							'release_type' => $this->input->post('release_type'),
							'release_download' => $_FILES['release_package']['name']
						);
						$this->project_model->add_release($data);
					}
					catch (Exception $ex) 
					{
						show_error('Database was not updated (Release::add) : ' 
									. $ex->getMessage());
					}
					
					# TODO: Create upload class that checks directory is writeable, 
					# and moves file, etc.
					$upload_path = APPPATH . 'uploads/';
					if (move_uploaded_file($_FILES['release_package']['tmp_name'],
										   $upload_path . $_FILES['release_package']['name']) )
					{
						$vars['page'] = 'success';
						$this->load->view('loader', $vars);
					}
					else 
					{
						show_error('File was not uploaded (database was updated though)');
					}
				}
			}
			else 
			{
				$vars['release_count'] = $this->project_model->get_release_count_by_alias($this->uri->segment(4));
				$vars['page'] = 'add_form';
				$this->load->view('loader', $vars);
			}
		}
		else 
		{
			$vars['projects'] = $this->project_model->get_all_projects();
			$this->load->view('loader', $vars);
		}
	}
	
	public function edit ()
	{
		$vars['area'] = 'admin';
		$vars['module'] = 'release/edit';
		$vars['theme_type'] = Theme::$theme_type;
		$vars['theme_alias'] = Theme::$theme_alias;
		
		# Got project alias.
		if ($this->uri->segment(4) !== FALSE) 
		{
			if ( $this->project_model->name === NULL )
			{
				show_error('That project does not exist.');
			}
			
			$vars['project'] = $this->project_model->get_project_by_alias($this->uri->segment(4));
			
			# We have a release number to work with.
			if ($this->uri->segment(5) !== FALSE)
			{
				if ($this->project_model->release_exists($this->uri->segment(5), $vars['project']->id) === FALSE)
				{
					show_error('The given release version does not exist for the given project.');
				}
				
				$vars['release'] = $this->project_model->get_release($this->uri->segment(5), $vars['project']->id);
				
				# Form submitted
				if ($this->input->post('edit_release') !== FALSE)
				{
					# Validation rules.
					$rules = array (
						array (
							'field' => 'project_name',
							'label' => 'Project Name',
							'rules' => 'required|trim|callback_project_exists'
						),
						array (
							'field' => 'release_codename',
							'label' => 'Release Codename',
							'rules' => 'trim|max_length[200]'
						),
						array (
							'field' => 'release_type',
							'label' => 'Release Type',
							'rules' => 'trim|callback_valid_release_type'
						),
						array (
							'field' => 'release_version',
							'label' => 'Release Version',
							'rules' => 'trim|required|max_length[20]'
						)
					);
					$this->form_validation->set_rules($rules);
					if ($this->form_validation->run() === FALSE)
					{
						$vars['release_count'] = $this->project_model->get_release_count_by_alias($this->uri->segment(4));
						$vars['page'] = 'edit_form';
						$this->load->view('loader', $vars);
					}
					else 
					{
						$data = array (
							'project_id' => $vars['project']->id,
							'release_codename' => $this->input->post('release_codename'),
							'release_version' => $this->input->post('release_version'),
							'release_type' => $this->input->post('release_type')
						);
						$this->project_model->update_release($vars['release']->id, $data);
					}
				}
				else 
				{
					$vars['release_count'] = $this->project_model->get_release_count_by_alias($this->uri->segment(4));
					$vars['page'] = 'edit_form';
					$this->load->view('loader', $vars);
				}
			}
			else 
			{
				$vars['releases'] = $this->project_model->get_releases($vars['project']->id);
				$vars['page'] = 'view_releases';
				$this->load->view('loader', $vars);
			}
		}
		else 
		{
			$vars['projects'] = $this->project_model->get_all_projects();
			$this->load->view('loader', $vars);
		}
	}
	
	/**
	 * VALIDATION CALLBACKS
	 * 
	 * For lack of a better way, they're in here.
	 */
	 
	 public function project_exists ($project)
	 {
	 	if ($this->project_model->get_project($project) === NULL)
		{
			$this->form_validation->set_message('project_exists', 'The given project does not exist!');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	 }
	 
	 /**
	  * Valid_release_type
	  * 
	  * release_type is an ENUM type in the database. 
	  * Therefore, we only allow what is contained in the ENUM.
	  */
	 public function valid_release_type ($type)
	 {
	 	$valid_types = array('alpha', 'beta', 'stable');
		
		if (!in_array($type, $valid_types))
		{
			$this->form_validation->set_message('valid_release_type', 'You have not supplied a valid release type!');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	 }
	
}


?>